package cn.sxt.userscenter.user.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.user.dao.IAAdminsDao;
import cn.sxt.userscenter.user.dao.IAUsersDao;
import cn.sxt.userscenter.user.dao.IAUsersExtendDao;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersExtend;
import cn.sxt.userscenter.users.service.IUsersService;
/**
 * usersService的实现类
 * @author PC-CZ
 *
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService{
	
	@Resource
	private IAAdminsDao adminsDao;
	
	@Resource
	private IAUsersDao usersDao;
	
	@Resource
	private IAUsersExtendDao usersExtendDao;
	
	@Override
	public AAdmins findOneAdmins(Map<String, Object> condMap) {
		return this.adminsDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findCondListAdmins(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		//如果pageInfoUtil不为空
		if(pageInfoUtil != null){
			//查询分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());           
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return adminsList;
			
			
		}
		
		
		/* 查询不分页 */
		return this.adminsDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveAdminsService(AAdmins admins) {
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.save(admins);
		if(res>0){
			//保存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","成功");
		}else{
			resultJSON.put("code", "2");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJOSN = new JSONObject();
		int res = this.adminsDao.update(admins);
		if(res>0){
			//储存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effect", res);
			
			resultJOSN.put("data", dataJSON);
			resultJOSN.put("code", "0");
			resultJOSN.put("info", "成功");
		}else{
			resultJOSN.put("code", "1");
			resultJOSN.put("info", "失败");
			
		}
		return resultJOSN;
	}

	
/*======================================================================================*/	
	
	
	@Override
	public AUsers findOneUsers(Map<String, Object> condMap) {
		return this.usersDao.findOne(condMap);
	}

	@Override
	public List<AUsers> findCondListUsers(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		//如果pageInfoUtil不为空
		if(pageInfoUtil != null){
			//查询分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());           
			List<AUsers> usersList = this.usersDao.findCondList(condMap);
			
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersList;
			
			
		}
		
		
		/* 查询不分页 */
		return this.usersDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveUsersService(AUsers users) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersDao.save(users);
		if(res>0){
			//保存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","成功");
			
			//还要保存用户的扩展信息
			AUsersExtend usersExtend = new AUsersExtend();
			usersExtend.setUserId(users.getId());
			usersExtend.setCreateTime(new Date());
			usersExtend.setUpdateTime(new Date());
			users.setUsersExtend(usersExtend);
			
			//在储存完一条用户信息以后更细用户扩展表  并且将要用户扩展表也保存
			this.saveUsersExtendService(usersExtend);
		}else{
			resultJSON.put("code", "2");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersService(AUsers users) {
		JSONObject resultJOSN = new JSONObject();
		int res = this.usersDao.update(users);
		if(res>0){
			//储存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJOSN.put("data", dataJSON);
			resultJOSN.put("code", "0");
			resultJOSN.put("info", "成功");
		}else{
			resultJOSN.put("code", "1");
			resultJOSN.put("info", "失败");
			
		}
		return resultJOSN;
	}
	
	
/*======================================================================================*/	
	
	
	@Override
	public AUsersExtend findOneUsersExtend(Map<String, Object> condMap) {
		return this.usersExtendDao.findOne(condMap);
	}

	@Override
	public List<AUsersExtend> findCondListUsersExtend(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		//如果pageInfoUtil不为空
		if(pageInfoUtil != null){
			//查询分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());           
			List<AUsersExtend> usersList = this.usersExtendDao.findCondList(condMap);
			
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			return usersList;
			
			
		}
		
		
		/* 查询不分页 */
		return this.usersExtendDao.findCondList(condMap);
	}

	@Override
	public JSONObject saveUsersExtendService(AUsersExtend users) {
		JSONObject resultJSON = new JSONObject();
		int res = this.usersExtendDao.save(users);
		if(res>0){
			//保存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info","成功");
		}else{
			resultJSON.put("code", "2");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneUsersExtendService(AUsersExtend users) {
		JSONObject resultJOSN = new JSONObject();
		int res = this.usersExtendDao.update(users);
		if(res>0){
			//储存数据
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", users.getId());
			dataJSON.put("effect", res);
			
			resultJOSN.put("data", dataJSON);
			resultJOSN.put("code", "0");
			resultJOSN.put("info", "成功");
		}else{
			resultJOSN.put("code", "1");
			resultJOSN.put("info", "失败");
			
		}
		return resultJOSN;
	}

}
